<template>
  <div class="dock-layout">
    <!-- 顶部栏 -->
    <header class="header">
      <div class="header-left">
        <img src="@/assets/logo.png" alt="Logo" class="logo" />
        <span class="app-name">Big ERP</span>
      </div>
      <div class="header-center">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="index" :to="item.path">
            {{ item.title }}
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="header-right">
        <el-input v-model="searchKeyword" placeholder="搜索..." clearable style="width: 300px;" size="small">
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-tooltip content="导航设置">
          <el-button circle @click="showNavigationSettings">
            <el-icon><Setting /></el-icon>
          </el-button>
        </el-tooltip>
        <el-dropdown @command="handleUserCommand">
          <el-avatar :size="36" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">个人中心</el-dropdown-item>
              <el-dropdown-item command="settings">系统设置</el-dropdown-item>
              <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <!-- 主内容区 -->
    <main class="content">
      <div class="content-wrapper">
        <router-view v-slot="{ Component }">
          <transition name="dock-fade" mode="out-in">
            <component :is="Component" :key="$route.fullPath" />
          </transition>
        </router-view>
      </div>
    </main>

    <!-- macOS风格的Dock栏 -->
    <div class="dock-container">
      <div class="dock-wrapper" @mousemove="handleDockMouseMove" @mouseleave="resetDock">
        <div class="dock-bg"></div>
        <div class="dock-items">
          <div
            v-for="(item, index) in dockItems"
            :key="item.path"
            class="dock-item"
            :class="{ active: isActive(item.path) }"
            :style="getDockItemStyle(index)"
            @click="navigateTo(item.path)"
            @mouseenter="hoveredIndex = index"
            @mouseleave="hoveredIndex = -1"
          >
            <div class="dock-icon" :style="{ background: item.color }">
              <el-icon :size="28" :style="getDockItemStyle(index)">
                <component :is="item.icon" />
              </el-icon>
            </div>
            <div class="dock-label">{{ item.title }}</div>
            <el-badge v-if="item.badge" :value="item.badge" class="dock-badge" />
            <!-- 活动指示器 -->
            <div v-if="isActive(item.path)" class="active-indicator"></div>
          </div>
        </div>
      </div>
    </div>

    <!-- Dock提示面板 - 已隐藏 -->
    <!-- 
    <transition name="fade-up">
      <div v-if="hoveredIndex >= 0" class="dock-tooltip" :style="getTooltipStyle()">
        <div class="tooltip-content">
          <h4>{{ dockItems[hoveredIndex]?.title }}</h4>
          <p>{{ dockItems[hoveredIndex]?.description }}</p>
          <div class="tooltip-shortcuts" v-if="dockItems[hoveredIndex]?.shortcut">
            <kbd>{{ dockItems[hoveredIndex].shortcut }}</kbd>
          </div>
        </div>
      </div>
    </transition>
    -->
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useNavigationStore } from '@/stores/navigation'
import { useUserStore } from '@/stores/user'
import { Search, Setting } from '@element-plus/icons-vue'
import { menuData } from '@/config/menu'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

const searchKeyword = ref('')
const hoveredIndex = ref(-1)
const iconSizes = ref([])

// 从菜单配置生成Dock项（选取重要模块，最多10-12个）
const dockItemsConfig = [
  { path: '/dashboard', color: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)', description: '数据概览和快速入口', shortcut: 'Ctrl+H' },
  { path: '/sales', color: 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)', description: '销售订单和客户管理', shortcut: 'Alt+1' },
  { path: '/inventory', color: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)', description: '商品和库存管理', shortcut: 'Alt+2' },
  { path: '/finance', color: 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)', description: '财务收支管理', shortcut: 'Alt+3' },
  { path: '/member', color: 'linear-gradient(135deg, #fa709a 0%, #fee140 100%)', description: '会员管理和营销', shortcut: 'Alt+4' },
  { path: '/reports', color: 'linear-gradient(135deg, #30cfd0 0%, #330867 100%)', description: '综合报表中心', shortcut: 'Alt+5' },
  { path: '/cockpit', color: 'linear-gradient(135deg, #a8edea 0%, #fed6e3 100%)', description: '数据驾驶舱', shortcut: 'Alt+6' },
  { path: '/extensions', color: 'linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%)', description: '插件和主题', shortcut: 'Alt+7' },
  { path: '/system', color: 'linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%)', description: '系统设置', shortcut: 'Alt+9' }
]

// 从menuData生成Dock项
const dockItems = computed(() => {
  return dockItemsConfig.map(config => {
    const menuItem = menuData.find(item => item.path === config.path)
    if (menuItem) {
      return {
        path: menuItem.path,
        title: menuItem.title,
        icon: ElementPlusIconsVue[menuItem.icon],
        color: config.color,
        badge: menuItem.badge || '',
        description: config.description,
        shortcut: config.shortcut
      }
    }
    return null
  }).filter(Boolean)
})

// 面包屑
const breadcrumbs = computed(() => {
  const matched = route.matched.filter(item => item.meta && item.meta.title)
  return matched.map(item => ({
    path: item.path,
    title: item.meta.title
  }))
})

const isActive = (path) => {
  return route.path.startsWith(path)
}

const navigateTo = (path) => {
  router.push(path)
}

// Dock悬停效果（类似macOS）- 优化版本，避免布局跳动
const handleDockMouseMove = (e) => {
  const dockWrapper = e.currentTarget
  const rect = dockWrapper.getBoundingClientRect()
  const mouseX = e.clientX - rect.left

  dockItems.value.forEach((item, index) => {
    const itemWidth = 70
    const itemCenter = (index + 0.5) * (itemWidth + 16)
    const distance = Math.abs(mouseX - itemCenter)
    const maxDistance = 120 // 减小影响范围
    
    if (distance < maxDistance) {
      const scale = 1 + (1 - distance / maxDistance) * 0.3 // 减小缩放幅度
      iconSizes.value[index] = scale
    } else {
      iconSizes.value[index] = 1
    }
  })
}

const resetDock = () => {
  iconSizes.value = []
}

const getDockItemStyle = (index) => {
  const scale = iconSizes.value[index] || 1 // 默认缩放比例
  return {
    transform: `scale(${scale})`, // 使用transform scale
    transition: 'transform 0.3s ease'
  }
}

// getTooltipStyle函数已移除，因为提示框已隐藏

const showNavigationSettings = () => {
  // 触发导航设置对话框
  window.dispatchEvent(new CustomEvent('show-navigation-settings'))
}

const handleUserCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      const userStore = useUserStore()
      userStore.logout()
      router.push('/login')
      ElMessage.success('已退出登录')
    }).catch(() => {})
  } else if (command === 'profile') {
    router.push('/system/user-settings')
  } else if (command === 'settings') {
    router.push('/system/settings')
  }
}
</script>

<style scoped>
.dock-layout {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  background: #f0f2f5;
  overflow: hidden;
  position: relative;
  /* 确保布局稳定性 */
  contain: layout style paint;
}

.header {
  height: 60px;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(20px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  z-index: 999;
  flex-shrink: 0; /* 防止header被压缩 */
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  height: 36px;
}

.app-name {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.header-center {
  flex: 1;
  padding: 0 40px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* 修复滚动问题 */
.content {
  flex: 1;
  min-height: 0; /* 关键：修复flex布局滚动问题 */
  overflow-y: auto;
  overflow-x: hidden;
  background: #f0f2f5;
  padding: 20px;
  position: relative;
  -webkit-overflow-scrolling: touch; /* iOS平滑滚动 */
  scroll-behavior: smooth; /* 平滑滚动 */
  /* 优化滚动性能 */
  will-change: scroll-position;
  contain: layout style paint;
}

/* 自定义滚动条 */
.content::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.content::-webkit-scrollbar-track {
  background: transparent;
}

.content::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 4px;
  transition: background 0.3s;
}

.content::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
}

/* 内容包装器，为Dock留出底部空间 */
.content-wrapper {
  width: 100%;
  padding: 24px;
  padding-bottom: 160px; /* 为Dock留出更多空间，避免重叠 */
  box-sizing: border-box;
  min-height: calc(100vh - 60px); /* 确保内容区域高度 */
}

/* Dock容器 */
.dock-container {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1500;
  pointer-events: none; /* 避免阻止内容区域的交互 */
  width: auto;
  max-width: 90vw;
}

.dock-wrapper {
  position: relative;
  padding: 12px 24px;
  pointer-events: all; /* 恢复Dock自身的交互 */
}

.dock-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(20px) saturate(150%);
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
  transition: all 0.3s ease;
}

.dock-items {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 16px;
  padding: 8px;
}

.dock-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  /* 固定尺寸，避免布局跳动 */
  width: 70px;
  height: 70px;
  flex-shrink: 0;
}

.dock-icon {
  width: 70px;
  height: 70px;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
  /* 确保图标容器尺寸固定 */
  position: relative;
  overflow: hidden;
}

.dock-item:hover .dock-icon {
  transform: translateY(-10px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
}

.dock-label {
  position: absolute;
  bottom: -24px;
  font-size: 12px;
  color: #606266;
  font-weight: 500;
  white-space: nowrap;
  opacity: 0;
  transform: translateY(5px);
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.95);
  padding: 4px 12px;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.dock-item:hover .dock-label {
  opacity: 1;
  transform: translateY(0);
}

.dock-badge {
  position: absolute;
  top: -8px;
  right: -8px;
}

.active-indicator {
  position: absolute;
  bottom: -12px;
  left: 50%;
  transform: translateX(-50%);
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #409eff;
  box-shadow: 0 0 8px rgba(64, 158, 255, 0.6);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: translateX(-50%) scale(1);
    opacity: 1;
  }
  50% {
    transform: translateX(-50%) scale(1.2);
    opacity: 0.8;
  }
}

/* Dock提示 - 已移除相关样式 */

/* 动画 */
.dock-fade-enter-active,
.dock-fade-leave-active {
  transition: all 0.4s ease;
}

.dock-fade-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.dock-fade-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

/* fade-up动画已移除，因为提示框已隐藏 */

/* 响应式 */
@media (max-width: 1200px) {
  .dock-items {
    gap: 12px;
  }
  
  .dock-icon {
    width: 60px;
    height: 60px;
  }

  .content-wrapper {
    padding: 20px;
    padding-bottom: 140px;
  }
}

@media (max-width: 768px) {
  .header-center {
    display: none;
  }
  
  .dock-container {
    bottom: 12px;
    max-width: 95vw;
  }
  
  .dock-items {
    gap: 8px;
  }
  
  .dock-icon {
    width: 50px;
    height: 50px;
    border-radius: 14px;
  }

  .dock-wrapper {
    padding: 8px 16px;
  }

  .content-wrapper {
    padding: 16px;
    padding-bottom: 120px;
  }
}
</style>


